/*
 * $Id: BaseDomParser.java,v 1.2 2009/06/22 15:32:51 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.util.xml.dom;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.ge.healthcare.autosc.common.ASCUtil;

public class BaseDomParser implements IXmlParser {

	private static final String JAXP_SCHEMA_LANGUAGE = 
		"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
	
	private static final String W3C_XML_SCHEMA = 
		"http://www.w3.org/2001/XMLSchema";
	
	private static final String JAXP_SCHEMA_SOURCE =
		"http://java.sun.com/xml/jaxp/properties/schemaSource";
	
	protected DocumentBuilder builder = null;
	
	protected ErrorHandler errHandler = null;
	
	public BaseDomParser(String schemaSource) throws ParserConfigurationException {
		createBuilder(schemaSource);
	}
	
	public BaseDomParser(String schemaSource, 
						 ErrorHandler errHandler) 
		throws ParserConfigurationException {
		this.errHandler = errHandler;
		createBuilder(schemaSource);
	}
	
	public void setErrorHandler(ErrorHandler errHandler) {
		builder.setErrorHandler(errHandler);
	}
	
	protected void createBuilder(String schemaSource) throws ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(true);
		factory.setNamespaceAware(true);
		factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
		factory.setAttribute(JAXP_SCHEMA_SOURCE, new File(schemaSource));
		
		builder = factory.newDocumentBuilder();
		if(!ASCUtil.isNullOrEmptyObject(errHandler)) {
        	builder.setErrorHandler(errHandler);
        }
	}
	
	public Document processResponse(InputSource is) 
		throws SAXException, IOException  {
		return builder.parse(is);
	}

	public Document processResponse(String xmlFileStr)
		throws SAXException, IOException {
		File xmlFile = new File(xmlFileStr);
		return processResponse(xmlFile);
	}
	
	public Document processResponse(File xmlFile)
		throws SAXException, IOException {
		InputStream is = null;
		Document document = null;
		try {
			is = new FileInputStream(xmlFile);
			document = builder.parse(is);
		} finally {
			if(is != null) {
				is.close();
			}
		}
		return document;
	}
}
